package com.api.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;

import com.bin.common.utils.StringUtils;

public class Config {

	public volatile static boolean QUIT_FLAG = false;
	public static String ROOT_DIR = null;
	

	
	
	public static long LOOP_TIMES = -1L;
	public static long SLEEP_MILLI = -1L;
	public static boolean IS_CREATE_SERVER = false;
	public static int CLIENT_PORT = -1;
	public static String CLIENT_USER_NAME = null;
	public static String CLIENT_PASSWORD = null;
	public static String SERVER_IP = null;
	public static int SERVER_PORT = -1;
	public static String LOGIN_NAME = null;
	public static String LOGIN_PASSWORD = null;
	public static String SVR_NODE = null;
	public static String SERVICE_CODE = null;
	public static int SERVICE_ID = -1;
	public static String SEND_PORT = null;
	public static int REPORT_FLAG = -1;
	public static boolean IS_NUM_PREFIX = true;
	public static String NUM_PREFIX = null;
	public static String SMS_CONTENT = null;


	public static boolean TEST_SPEED = false;
	public static boolean TEST_REPORT = false;

	public volatile static long reportCnt = 0;
	public volatile static long reportCntTmp = 0;
	public static Date reportStratDate = null;
	
	public static void init(String fileName) {
		if(fileName == null){
			fileName = "gtf.properties";
		}
		ROOT_DIR = System.getProperty("user.dir");
		Properties properties = new Properties();
		// 使用ClassLoader加载properties配置文件生成对应的输入流
		BufferedReader bufferedReader = null;
		// 使用properties对象加载输入流
		try {
			bufferedReader = new BufferedReader(new FileReader(ROOT_DIR+File.separator+fileName));
			properties.load(bufferedReader);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			if (bufferedReader != null) {
				
				try {
					bufferedReader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
				
			}
		}
		String loopTimeStr = ((String) properties.get("loopTimes")).trim();
		LOOP_TIMES 		= StringUtils.isEmpty(loopTimeStr) ? -1L : StringUtils.tolong(loopTimeStr);
		String sleepMilli = ((String) properties.get("sleepMilli")).trim();
		SLEEP_MILLI 	= StringUtils.isEmpty(sleepMilli) ? -1L : StringUtils.tolong(sleepMilli);
		String isCreateServer = (String) properties.get("isCreateServer");
		IS_CREATE_SERVER = StringUtils.isEmpty(isCreateServer) ? false : StringUtils.equals("1", isCreateServer);
		String clientPort=	((String) properties.get("clientPort")).trim();
		CLIENT_PORT		 = StringUtils.isEmpty(clientPort) ? 8901 : StringUtils.toInteger(clientPort);
		CLIENT_USER_NAME = (String) properties.get("clientUsername");
		
		CLIENT_PASSWORD = ((String) properties.get("clientPassword")).trim();
		SERVER_IP 		= ((String) properties.get("serverIp")).trim();
		String serverPort = ((String) properties.get("serverPort")).trim();
		SERVER_PORT 	= StringUtils.isEmpty(serverPort) ? 8901 : StringUtils.toInteger(serverPort);
		LOGIN_NAME 		= ((String) properties.get("loginName")).trim();
		LOGIN_PASSWORD 	= ((String) properties.get("loginPassword")).trim();
		SVR_NODE 		= ((String) properties.get("srvNode")).trim();
		SERVICE_CODE	= ((String) properties.get("serviceCode")).trim();		
		String serviceId = ((String) properties.get("serviceId")).trim();
		SERVICE_ID 	= StringUtils.isEmpty(serviceId) ? 100002 : StringUtils.toInteger(serviceId);
		SEND_PORT 		= ((String) properties.get("sendPort")).trim();
		String reportFlag = ((String) properties.get("reportFlag")).trim();
		REPORT_FLAG 	= StringUtils.isEmpty(reportFlag) ? 0 : StringUtils.toInteger(reportFlag);
		String isNumPrefix = ((String) properties.get("isNumPrefix")).trim();
		IS_NUM_PREFIX = StringUtils.isEmpty(isNumPrefix) ? false : StringUtils.equals("true", isNumPrefix);
		NUM_PREFIX 		= ((String) properties.get("numPrifix")).trim();
		if(NUM_PREFIX.length() > 3 && IS_NUM_PREFIX){
			NUM_PREFIX = NUM_PREFIX.substring(0,3);
		}
		SMS_CONTENT 	= ((String) properties.get("smscontent")).trim();
		
			try {
				SMS_CONTENT = new String("测试GBK符号，是否可以通过.".getBytes("utf-8"),"utf-8");
				SMS_CONTENT = new String(SMS_CONTENT.getBytes("GBK"),"GBK");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
		
		String testSpeed = (String) properties.get("testSpeed");
		TEST_SPEED = StringUtils.isEmpty(testSpeed) ? false : StringUtils.equals("true", testSpeed);
		
		String testReportSpeed = (String) properties.get("testReportSpeed");
		TEST_REPORT = StringUtils.isEmpty(testReportSpeed) ? false : StringUtils.equals("true", testReportSpeed);
		
		System.out.println("Properties 加载完成");
	}
	
	
	public static void main(String[] args){
		String str = "135";
		int val = 78;
		String abc = String.format("%08d", val);
		System.out.println(str+abc);
	}
	
	
}
